package com.la.bc_api.service.impl;

import com.la.bc_api.service.APITestService;
import com.la.bc_api.utils.httpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @Author LA
 * @Date 2024/8/29 14:19
 * @Description:
 */
@Slf4j
@Service
public class APITestServiceImpl implements APITestService {

    /**
     * 智能体请求
     * @param message
     * @return
     */
    @Override
    public String creatGPT(String message) {
        //创建 Assistant File


        //创建 Assistant
        //测试新建的分支。。。。。。111
        //创建 Thread

        //创建 Message

        //创建 run

        //查看运行状态

        //查看Message列表获取结果

        return null;
    }

    /**
     * 通用请求
     * @param message
     * @return
     */
    @Override
    public String request(String message) {
        String url="https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions";
        //构建请求体
        String jsonBody= """
                {
                	"model":"qwen-turbo",
                	"messages": [
                       {
                           "role": "system",
                           "content": "You are a helpful assistant."
                       },
                       {
                           "role": "user",
                           "content" : "%s"
                       }
                    ]
                }
                """.formatted(message);

        String entity = null;
        CloseableHttpResponse response = httpClientUtil.doPost(url, jsonBody);
        int statusCode = response.getStatusLine().getStatusCode();
        if(statusCode == 200){
            try {
                entity = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            log.info("请求成功 ： {}",entity);
            return entity;
        }else{
            try {
                entity = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            log.warn("请求失败:Body:{}",entity);
            return entity;
        }
    }
}














